/**
 * @param {number[]} piles
 * @param {number} h
 * @return {number}输入：piles = [3,6,7,11], h = 8
输出：4
 */
var minEatingSpeed = function (piles, h) {
  const calcTime = (data, k) => {
    const datas = data / k
    let times = Math.floor(datas)
    return datas > times ? times + 1 : times
  }
  if (piles.length === 1) {
    return calcTime(piles[0], h)
  }
  const maxPile = Math.max(...piles)
  let ks = 0

  const eat = (piles, h, max, min) => {
    let time = 0
    let k = Math.floor((max + min) / 2)
    piles.forEach(item => {
      time += calcTime(item, k)
    })
    if (Math.abs(max - min) <= 1) {
      ks = time > h ? k + 1 : k
      return
    }
    time > h ? eat(piles, h, max, k) : eat(piles, h, k, min)
  }
  eat(piles, h, maxPile, 0)
  return ks
}
console.log(
  minEatingSpeed(
    [
      332484035, 524908576, 855865114, 632922376, 222257295, 690155293, 112677673, 679580077, 337406589, 290818316, 877337160, 901728858,
      679284947, 688210097, 692137887, 718203285, 629455728, 941802184
    ],
    823855818
  )
)
